package com.fenxiangle.yueding.feature.mine.view.adapter;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.aliyun.vod.common.utils.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.common.util.DateTool;
import com.fenxiangle.yueding.common.widget.HorizontalRecyclerView;
import com.fenxiangle.yueding.entity.OrderBtnType;
import com.fenxiangle.yueding.entity.OrderType;
import com.fenxiangle.yueding.entity.bo.PublishDemandBo;
import com.fenxiangle.yueding.entity.bo.ReceviedState;
import com.fenxiangle.yueding.feature.home.view.UserInfoActivity;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.utils.StringUtil;

import java.util.List;

public class PublishOrderAdapter extends BaseQuickAdapter<PublishDemandBo, BaseViewHolder> {

    public PublishOrderAdapter() {
        super(R.layout.item_publish_order);
    }

    @Override
    protected void convert(BaseViewHolder helper, PublishDemandBo item) {

        AM.image().bindToCircleObject(item.getOwnUserHead(), helper.getView(R.id.iv_my_head));
        LinearLayout ll_publish_user_head = helper.getView(R.id.ll_publish_user_head);
        ll_publish_user_head.removeAllViews();
        if (item.getGrabUser() != null && item.getGrabUser().size() > 0) {
            for (int i = 0; i < item.getGrabUser().size(); i++) {
                ImageView imageView = new ImageView(mContext);
//            imageView.setTag(item.getGrabUser().get(i).getUserUid());
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(dp2px(30),
                        dp2px(30));
                layoutParams.setMargins(0, 0, dp2px(10), 0);
                imageView.setLayoutParams(layoutParams);
                AM.image().bindToCircleObject(item.getGrabUser().get(i).getUserHead(), imageView);
                int finalI = i;
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, UserInfoActivity.class);
                        intent.putExtra("id", item.getGrabUser().get(finalI).getUserUid());
                        mContext.startActivity(intent);
                    }
                });
                ll_publish_user_head.addView(imageView);
            }
        }

        //Html.fromHtml("" + (item.getType() == 1 ? "抢单" : "邀约单") + "--<font color='#ffd565'><b><tt>" + OrderType.getName(item.getOrderStatus()) + "</tt></b></font>") :
        //                        Html.fromHtml("" + + "--<font color='#6E6E6E'>" + OrderType.getName(item.getOrderStatus()) + "</font>")
        helper.setText(R.id.tv_my_publish_title, Html.fromHtml("<b><tt>" + (item.getType() == 1 ? "需求内容" : "邀约内容") + "：</tt></b>" + "人数" + item.getNumPeople() + "人。 " + item.getOrderDescribe()))
                .setText(R.id.tv_my_publish_date, "时间：" + DateTool.stampToDate(item.getStartTime()) + "——" + DateTool.stampToDate(item.getEndTime()))
                .setText(R.id.tv_my_publish_address, "地点：" + item.getAgreedPlace())
                .setText(R.id.tv_my_publish_credit, "信用金：￥100")
                .setText(R.id.tv_my_publish_stauts, StringUtil.setTextViewColor((item.getOrderStatus() == 6 ? (item.getType() == 1 ? "抢单" : "邀约单") + "-" + OrderType.getName(item.getOrderStatus()) :
                                (item.getType() == 1 ? "抢单" : "邀约单") + "-" + OrderType.getName(item.getOrderStatus())) + (item.getType() == 1 ? "(" + (item.getGrabUser() != null && item.getGrabUser().size() > 0 ? item.getGrabUser().size() : "0") + "人)" : ""),
                        OrderType.getName(item.getOrderStatus()) + (item.getType() == 1 ? "(" + (item.getGrabUser() != null && item.getGrabUser().size() > 0 ? item.getGrabUser().size() : "0") + "人)" : ""), mContext.getResources().getColor(R.color.publish_order_status)))
                .setText(R.id.tv_my_publish_money, "服务费：￥" + item.getTotalPrice())
                .setText(R.id.tv_my_publish_order_no, "订单号：" + item.getOrderId())
                .setText(R.id.btn_order_ok, OrderBtnType.getName(item.getOrderStatus()))
                .setText(R.id.tv_my_publish_occupation, "职业: " + item.getOccupation())
                .addOnClickListener(R.id.btn_my_publish_activist)
                .addOnClickListener(R.id.btn_order_ok)
                .addOnClickListener(R.id.btn_call_phone)
                .addOnClickListener(R.id.btn_copy_wx)
                .addOnClickListener(R.id.btn_publis_toweiquan)
                .addOnClickListener(R.id.btn_publis_querendingdan)
                .addOnClickListener(R.id.iv_my_publish_head)
                .addOnClickListener(R.id.tv_my_publish_username)
        ;
        if (item.getType() == 1) {
            helper.getView(R.id.tv_my_publish_occupation).setVisibility(View.VISIBLE);
        } else {
            helper.getView(R.id.tv_my_publish_occupation).setVisibility(View.GONE);

        }
        if (TextUtils.isEmpty(OrderBtnType.getName(item.getOrderStatus()))) {
            helper.getView(R.id.btn_order_ok).setVisibility(View.GONE);
        } else {
            helper.getView(R.id.btn_order_ok).setVisibility(View.VISIBLE);
        }

        if (item.getOrderStatus() == 4) {
            if (item.getType() == 1) {
                helper.getView(R.id.btn_show_wq).setVisibility(View.GONE);
                helper.getView(R.id.btn_order_ok).setVisibility(View.VISIBLE);
                helper.getView(R.id.btn_my_publish_activist).setVisibility(View.VISIBLE);
            } else {
                helper.getView(R.id.btn_show_wq).setVisibility(View.VISIBLE);
                helper.getView(R.id.btn_order_ok).setVisibility(View.GONE);
                helper.getView(R.id.btn_my_publish_activist).setVisibility(View.GONE);
            }
        } else if (item.getOrderStatus() == 3) {
            if (item.getType() == 1) {//可抢
                helper.getView(R.id.btn_show_wq).setVisibility(View.GONE);
                helper.getView(R.id.btn_order_ok).setVisibility(View.VISIBLE);
                helper.getView(R.id.btn_my_publish_activist).setVisibility(View.GONE);
                helper.setText(R.id.btn_order_ok, "查看");
            } else {//
                helper.getView(R.id.btn_show_wq).setVisibility(View.GONE);
                helper.setText(R.id.btn_order_ok, "取消订单");
                helper.getView(R.id.btn_order_ok).setVisibility(View.VISIBLE);
                helper.getView(R.id.btn_my_publish_activist).setVisibility(View.GONE);
            }
        } else if (item.getOrderStatus() == 1) {
            helper.getView(R.id.btn_order_ok).setVisibility(View.GONE);
        } else {
            if (item.getType() == 1) {//抢单
                helper.getView(R.id.btn_my_publish_activist).setVisibility(View.GONE);
                helper.getView(R.id.btn_show_wq).setVisibility(View.GONE);
            } else {
                helper.getView(R.id.btn_show_wq).setVisibility(View.GONE);
                helper.getView(R.id.btn_my_publish_activist).setVisibility(View.GONE);
            }
        }

//        if (item.getOrderStatus() == -1 || item.getOrderStatus() == 0 || item.getOrderStatus() == 2 || item.getOrderStatus() == 4) {
//            helper.getView(R.id.btn_copy_wx).setVisibility(View.GONE);
//            helper.getView(R.id.btn_call_phone).setVisibility(View.GONE);
//        } else {
//            helper.getView(R.id.btn_copy_wx).setVisibility(View.VISIBLE);
//            helper.getView(R.id.btn_call_phone).setVisibility(View.VISIBLE);
//        }
    }

    /**
     * dp to px
     */
    protected int dp2px(float dp) {
        final float scale = mContext.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }
}
